package com.mvvm.ktnet.ext

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.rxLifeScope
import com.mvvm.ktnet.base.viewmodel.*

import kotlinx.coroutines.*
import rxhttp.wrapper.exception.ParseException


fun BaseViewModel.launchCheckNoStatus(
    block: suspend CoroutineScope.() -> Unit,
    onEmpty: ((Throwable) -> Unit) = {},
    onError: ((Throwable) -> Unit) = {},
    onStart: (() -> Unit) = { },
    onFinally: (() -> Unit) = {}
): Job {
    return rxLifeScope.launch(
        block,
        {
            if (it is ParseException) {
                if (it.errorCode == "2") onEmpty(it)
                else onError(it)
            } else {
                onError(it)
            }
        },
        onStart,
        onFinally
    )
}

/**
 * @param block     协程代码块，运行在UI线程
 * @param onError   异常回调，运行在UI线程
 * @param onStart   协程开始回调，运行在UI线程
 * @param onFinally 协程结束回调，不管成功/失败，都会回调，运行在UI线程
 */
fun BaseViewModel.launchNoEmpty(
    block: suspend CoroutineScope.() -> Unit,
    onError: ((Throwable) -> Unit) = {},
    onStart: (() -> Unit) = {},
    onFinally: (() -> Unit) = {},
    mathCode: Int = 0,
    loadingType: LoadingType = LoadingType.LOADING_NULL,
    loadingMessage: String = "请求网络中..."
): Job {

    return launchCheckNoStatus(
        block,
        {
            loadStatus.loadEmpty.postValue(LoadStatusBean(mathCode, it))
            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = true,
                    error = false
                )
        },
        {
            loadStatus.loadError.postValue(LoadStatusBean(mathCode, it))
            onError.invoke(it)
            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = false,
                    error = true
                )

        },
        {


            onStart.invoke()
            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = false,
                    error = false
                )

        },
        {

            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = loadingChange.showPageLoad.value?.empty== true||loadingChange.showPageLoad.value?.error == true,
                    empty = loadingChange.showPageLoad.value?.empty== true,
                    error = loadingChange.showPageLoad.value?.error == true
                )
            loadStatus.loadFinally.postValue(LoadFinally(mathCode))
            onFinally.invoke()
        }
    )

}

/**
 * @param block     协程代码块，运行在UI线程
 *@param  onEmpty   空代码数据2回调，运行在UI线程
 * @param onError   异常回调，运行在UI线程
 * @param onStart   协程开始回调，运行在UI线程
 * @param onFinally 协程结束回调，不管成功/失败，都会回调，运行在UI线程
 */
fun BaseViewModel.launchEmpty(
    block: suspend CoroutineScope.() -> Unit,
    onEmpty: ((Throwable) -> Unit) = {},
    onError: ((Throwable) -> Unit) = {},
    onStart: (() -> Unit) = {},
    onFinally: (() -> Unit) = {},
    mathCode: Int = 0,
    loadingType: LoadingType = LoadingType.LOADING_NULL,
    loadingMessage: String = "请求网络中..."
): Job {
    return launchCheckNoStatus(block,
        {
            loadStatus.loadEmpty.postValue(LoadStatusBean(mathCode, it))
            onEmpty.invoke(it)
            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = true,
                    error = false
                )

        },
        {
            loadStatus.loadError.postValue(LoadStatusBean(mathCode, it))
            onError.invoke(it)
            loadingChange.showPageLoad.postValue(
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = false,
                    error = true
                )
            )
        },
        {

            onStart.invoke()
            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = true,
                    empty = false,
                    error = false
                )

        },
        {


            loadingChange.showPageLoad.value=
                LoadingPageEntity(
                    loadingType,
                    loadingMessage,
                    show = loadingChange.showPageLoad.value?.empty== true||loadingChange.showPageLoad.value?.error == true,
                    empty = loadingChange.showPageLoad.value?.empty== true,
                    error = loadingChange.showPageLoad.value?.error == true
                )


            loadStatus.loadFinally.postValue(LoadFinally(mathCode))
            onFinally.invoke()
        }
    )
}
